<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
			
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = []
				if(fileId){
					ajaxArr.push({
						url:'/salesDelivery/getValue',
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					if(_that.subEditorObj["title2"]){
						let attr = {
							showAdd:false,
							showDel:false
						}
						let configData = _that.subConfigData["title2"]
						if(hasEditor("title2","newProductName",configData)){
							let title = getEditorTitle("title2","newProductName",configData)
							attr.titleTemplate = title+':<%= newProductName %>'
						}
						changeEditor("title2","productStr",attr,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push('id')
						for(let index in res[0].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[0].value[index])
								_that.$set(_that.formdata,index,res[0].value[index])
							}
						}
						if(_that.subEditorObj['title2']){
							let productList = res[0].value.productList
							for(let item of productList){
								_that.$set(item,'outBatchList',null)
								_that.$set(item,'batchList',null)
							}
							_that.subEditorObj["title2"].workList = productList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "productStr", JSON.stringify(productList))
							_that.$set(_that.formdata, "productStr", JSON.stringify(productList))
						}
						if(res[0].value.reviewerList){
							let reviewerList = res[0].value.reviewerList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "reviewerStr", JSON.stringify(reviewerList))
							_that.$set(_that.formdata, "reviewerStr", JSON.stringify(reviewerList))
						}
					}
					_that.$nextTick(function(){
						
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				if(_that.checkedit){
					let productList = JSON.parse(_that.formdata.productStr) 
					for(let item of productList){
						if(!item.fileList){
							item.fileList = []
						}
						if(!item.batchList){
							item.batchList = []
						}
						if(!item.outBatchList){
							item.outBatchList = []
						}
					}
					_that.$set(_that.formdata,'productStr',JSON.stringify(productList))
					_that.om.formSave("salesDelivery/doReviewUpdate",_that.formdata,_that)
				}
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
			}
		}
	}
</script>

<style>
</style>
       